package clientereligadora.formularios;

/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */

/*
 * frm_Login.java
 *
 * Created on 17/07/2011, 10:14:23
 */
import clientereligadora.conexao.Conexao;
import clientereligadora.entidades.Usuario;
import clientereligadora.operacoes.Operacao;
 
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.basic.BasicComboBoxRenderer;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

/**
 *
 * @author Leandro Aquino
 */
public final class FormLogin extends javax.swing.JFrame {

    Conexao conn;
    Operacao consulta = new Operacao();
    String senha;
    String usuario;
    String senha_usuario;
    String user = null;
    // LAYOUT DE JOPTIONPANES
    Icon warning = new ImageIcon("C:\\cfgReligadora\\icons\\Warning.png");
    Icon ok = new ImageIcon("C:\\cfgReligadora\\icons\\OK.png");
    Icon erro = new ImageIcon("C:\\cfgReligadora\\icons\\Error.png");
    Icon info = new ImageIcon("C:\\cfgReligadora\\icons\\Infor.png");
    Icon questionIcon = new ImageIcon("C:\\cfgReligadora\\icons\\question.png");
    JLabel label1;
    FormPrincipal frmPrincipal;
    Operacao operacao = new Operacao();
    java.util.List<Usuario> listUsuarios;

    /**
     * Creates new form frm_Login
     */
    public FormLogin() {

        initComponents();


        //verifico dados para conexoa com DataBase

        validaConexao();

        //efeito gradiente no botao ENTRAR
        upGradient();
        label1 = new JLabel();

        btnEntrar.setForeground(new Color(249, 178, 49));

        // CENTRALIZA PAGINA
        setLocationRelativeTo(null);

        //mando o foco para o nome do usuario
        txtNome.requestFocus();

        //liminto o numero de caracteres para o campo senha
        pwdSenha.setDocument(new MaxLengthTextDocument());

        //altero os padroes de messagens exibidas no OptionPane
        UIManager.put("OptionPane.yesButtonText", "Sim");
        UIManager.put("OptionPane.noButtonText", "Não");
        UIManager.put("OptionPane.cancelButtonText", "Cancelar");
        UIManager.put("OptionPane.informationIcon", info);
        UIManager.put("OptionPane.errorIcon", erro);
        UIManager.put("OptionPane.questionIcon", questionIcon);
        UIManager.put("OptionPane.warningIcon", warning);
        UIManager.getDefaults().put("OptionPane.background", new Color(255, 255, 255));
        UIManager.put("Panel.background", new Color(255, 255, 255));


        BasicComboBoxRenderer.UIResource UIResource = new BasicComboBoxRenderer.UIResource();
        UIResource.setHorizontalAlignment(SwingConstants.CENTER);

        pwdSenha.setHorizontalAlignment(javax.swing.JTextField.CENTER);

    }

    public void validaConexao() {

        String ip = "";
        String port = "";
        String database = "";
        String user = "";
        String passwd = "";

        //caminho para arquivo de configuração
        String config = "C:\\cfgReligadora\\config\\server.txt";
        try {
            //abre arquivo
            FileReader reader = new FileReader(config);
            //buferiza arquivo
            BufferedReader leitor = new BufferedReader(reader);

            //le configuração
            String linha = null;
            int counter = 1;
            try {
                while ((linha = leitor.readLine()) != null) {
                    switch (counter) {
                        case 1:
                            ip = linha;
                            break;
                        case 2:
                            port = linha;
                            break;
                        case 3:
                            database = linha;
                            break;
                        case 4:
                            user = linha;
                            break;
                        case 5:
                            passwd = linha;
                            break;
                        default:
                            break;
                    }

                    counter++;
                }
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(this, "ERRO AO TENTAR LER LINHA \n"
                        + "FORM_LOGIN, LINHA:" + counter + "\n"
                        + ex, "ERRO DE CONEXÃO",
                        JOptionPane.ERROR_MESSAGE);
                System.exit(0);
            }

            try {
                leitor.close();
                reader.close();
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(this, "ERRO AO FECHA ARQUIVO DE CONFIGURAÇÃO\n"
                        + "FORM_LOGIN\n"
                        + ex, "ERRO DE CONEXÃO",
                        JOptionPane.ERROR_MESSAGE);
                System.exit(0);
            }


            conn = new Conexao(ip, port, database, user, passwd);
        } catch (FileNotFoundException ex) {
            JOptionPane.showMessageDialog(this, "NÃO FOI POSSIVEL ABRIR ARQUIVO DE CONFIGURAÇÃO\n"
                    + "FORM_LOGIN\n"
                    + ex, "ERRO DE CONEXÃO",
                    JOptionPane.ERROR_MESSAGE);
            System.exit(0);
        }

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        btnEntrar = new javax.swing.JButton();
        pwdSenha = new javax.swing.JPasswordField();
        jLabel7 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtNome = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("RELIGADORA");
        setIconImage(getToolkit().getImage("C:\\cfgReligadora\\logo.png"));
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setAutoscrolls(true);

        jLabel9.setBackground(new java.awt.Color(255, 255, 255));
        jLabel9.setFont(new java.awt.Font("Arial", 0, 9)); // NOI18N
        jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel9.setText(" ");

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/logoNastek.jpg"))); // NOI18N
        jLabel1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel1MouseClicked(evt);
            }
        });

        jLabel2.setBackground(new java.awt.Color(255, 255, 255));
        jLabel2.setFont(new java.awt.Font("Arial", 0, 11)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(0, 0, 255));
        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/usuario.png"))); // NOI18N

        jLabel3.setBackground(new java.awt.Color(255, 255, 255));
        jLabel3.setFont(new java.awt.Font("Arial", 0, 11)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(0, 0, 255));
        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/segur.png"))); // NOI18N

        btnEntrar.setFont(new java.awt.Font("Arial Black", 0, 11)); // NOI18N
        btnEntrar.setText("ENTRAR");
        btnEntrar.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(204, 204, 204), 1, true));
        btnEntrar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                btnEntrarMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                btnEntrarMouseExited(evt);
            }
        });
        btnEntrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEntrarActionPerformed(evt);
            }
        });
        btnEntrar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                btnEntrarKeyPressed(evt);
            }
        });

        pwdSenha.setFont(new java.awt.Font("Arial", 0, 10)); // NOI18N
        pwdSenha.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        pwdSenha.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                pwdSenhaKeyPressed(evt);
            }
        });

        jLabel7.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel7MouseClicked(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Arial", 0, 11)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(153, 153, 153));
        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/segur1.png"))); // NOI18N
        jLabel5.setText("Efetue Login para gerencias as Religadoras");

        txtNome.setFont(new java.awt.Font("Arial", 0, 10)); // NOI18N
        txtNome.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtNome.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtNomeKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jLabel1))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jLabel5))
                    .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(90, 90, 90)
                .addComponent(jLabel2)
                .addGap(1, 1, 1)
                .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(90, 90, 90)
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, 0)
                .addComponent(pwdSenha, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, 0)
                .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(52, 52, 52)
                .addComponent(btnEntrar, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel5))
                    .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(41, 41, 41)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(pwdSenha, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(37, 37, 37)
                .addComponent(btnEntrar, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(31, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnEntrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEntrarActionPerformed
        user = txtNome.getText();
        senha = pwdSenha.getText();
        if (user.equalsIgnoreCase("") || user == null) {
            JOptionPane.showMessageDialog(null, "CAMPO NOME EM BRANCO");
            txtNome.requestFocus();
        } else if (senha.equalsIgnoreCase("") || senha == null) {
            JOptionPane.showMessageDialog(null, "CAMPO SENHA EM BRANCO");
            pwdSenha.requestFocus();
        } else {
            MyThread mt = new MyThread();
            mt.start();
        }
       
    }//GEN-LAST:event_btnEntrarActionPerformed

    private void btnEntrarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_btnEntrarKeyPressed
        int key = evt.getKeyCode();

        if (key == KeyEvent.VK_ENTER) {

            MyThread mt = new MyThread();
            mt.start();
        }

    }//GEN-LAST:event_btnEntrarKeyPressed

    public void verificaps() {
        jLabel9.setFont(new java.awt.Font("Arial", 0, 9)); // NOI18N

        Toolkit tk = Toolkit.getDefaultToolkit();

        if (tk.getLockingKeyState(KeyEvent.VK_CAPS_LOCK)) {
            Icon ativo = new ImageIcon("C:\\cfgReligadora\\icons\\atencao_caps.png");
            jLabel9.setIcon(ativo);
            // jLabel9.setText("Caps Ativada");

        } else {

            Icon ativo = new ImageIcon();
            jLabel9.setIcon(ativo);
            //  jLabel9.setText("");
        }

    }

    public void verificaps1() {
        label1.setFont(new java.awt.Font("Arial", 0, 9)); // NOI18N

        Toolkit tk = Toolkit.getDefaultToolkit();

        if (tk.getLockingKeyState(KeyEvent.VK_CAPS_LOCK)) {
            Icon ativo = new ImageIcon("C:\\cfgReligadora\\icons\\atencao_caps.png");
            label1.setIcon(ativo);
            // label1.setText("Caps Ativada");

        } else {

            Icon ativo = new ImageIcon();
            label1.setIcon(ativo);
            label1.setText("");
        }

    }
    private void jLabel7MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel7MouseClicked
        //AQUI CHAMO O METODO PARA VERIFICAR SE A TECLA CAPC LOCK ESTA ATIVADA
        verificaps1();

        //CRIO UM JPasswordField QUE NA HORA QUE O USUARIODIGITAR ALGUMA COISA ELE
        //TRANSFORMA O TEXTO EM UM *
        JPasswordField passwd = new JPasswordField(10);
        passwd.setEchoChar('*');

        passwd.requestFocusInWindow();
        //CRIO UMA RESTRICAO PROIBINDO O USUARIO COLAR ALGUMA COISA NO CAMPO SENHA
        //define uma Action para ser passada como parametro para o getActionMap()  
        Action acao = new AbstractAction() {

            public void actionPerformed(ActionEvent e) {
                //aqui vai a ação a ser tomada quando o usuário digitar Ctrl + V  
                JOptionPane.showMessageDialog(null, "VOCE NAO PODE COLAR NADA AQUI", "ERRO", JOptionPane.QUESTION_MESSAGE);
            }
        };

        //adiciona uma entrada para o mapa e especifica uma key com o KeyStroke  
        passwd.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK), "CTRL_V");

        //obtem o mapa de acões do componente  
        passwd.getActionMap().put("CTRL_V", acao);


        //AQUI CHAMO O METODO PARA VERIFICAR SE A TECLA CAPC LOCK ESTA ATIVADA ENQUANTO O USUARIO DIGITA ALGUMA COISA
        passwd.addKeyListener(new java.awt.event.KeyAdapter() {

            @Override
            public void keyPressed(java.awt.event.KeyEvent evt) {

                verificaps1();
            }
        });

        //DEFINO O USUARIO QUE REQUER SENHA PARA CHAMAR PROXIMA TELA
        String usuario = "leandro";
        String senha;

        //ADICIONO AO PAINEL OS COMPONETES
        JLabel label = new JLabel("SENHA MASTER: ");

        JLabel label2 = new JLabel("");

        JPanel entUsuario = new JPanel();

        entUsuario.add(label);

        entUsuario.add(passwd);

        entUsuario.add(label1);


        JOptionPane.showMessageDialog(this, entUsuario, "INSIRA A SENHA DE ADMINISTRADOR", JOptionPane.QUESTION_MESSAGE);

        String senha_usuario = passwd.getText();

        //VERIFICO SENHA DO USUARIO

        if (senha_usuario == null) {
        } else {

            senha = "123";

            if (senha_usuario == null ? senha == null : senha_usuario.equals(senha)) {

                FormCadastroUsuario CadUser = new FormCadastroUsuario(conn);
                CadUser.setVisible(true);
                this.dispose();
            } else {
                JOptionPane.showMessageDialog(this, "SENHA INVALIDA OU CAMPO EM BRANCO",
                        "ERRO AO VALIDAR SENHA",
                        JOptionPane.INFORMATION_MESSAGE, erro);

            }
        }
    }//GEN-LAST:event_jLabel7MouseClicked

    private void pwdSenhaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_pwdSenhaKeyPressed

        verificaps();

        int key = evt.getKeyCode();

        if (key == KeyEvent.VK_ENTER) {

            MyThread mt = new MyThread();
            mt.start();
        }
    }//GEN-LAST:event_pwdSenhaKeyPressed

    private void jLabel1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel1MouseClicked
        try {
            try {
                Desktop.getDesktop().browse(new URI("http://www.nastek.com.br/site/"));
            } catch (URISyntaxException ex) {
                Logger.getLogger(FormLogin.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (IOException ex) {
            Logger.getLogger(FormLogin.class.getName()).log(Level.SEVERE, null, ex);
        }        // TODO add your handling code here:
    }//GEN-LAST:event_jLabel1MouseClicked

    private void btnEntrarMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnEntrarMouseEntered
        downGradient();
        setCursor(new Cursor(Cursor.HAND_CURSOR));
    }//GEN-LAST:event_btnEntrarMouseEntered

    private void btnEntrarMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnEntrarMouseExited
        upGradient();
        setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
    }//GEN-LAST:event_btnEntrarMouseExited

    private void txtNomeKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtNomeKeyPressed

        int key = evt.getKeyCode();

        if (key == KeyEvent.VK_ENTER) {
            pwdSenha.requestFocus();
        }
    }//GEN-LAST:event_txtNomeKeyPressed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {

                new FormLogin().setVisible(true);

            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEntrar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPasswordField pwdSenha;
    private javax.swing.JTextField txtNome;
    // End of variables declaration//GEN-END:variables

    public void validaUsuario() {

        try {
            usuario = txtNome.getText().toString().trim();

            senha = consulta.getPasswd(usuario);

            senha_usuario = pwdSenha.getText();

            Thread.sleep(2000);
        } catch (InterruptedException ex) {
            Logger.getLogger(FormLogin.class.getName()).log(Level.SEVERE, null, ex);
        }


        if (senha_usuario == null ? senha == null : senha_usuario.equals(senha)) {

            frmPrincipal = new FormPrincipal(usuario, conn);
            frmPrincipal.setVisible(true);
            this.setVisible(false);

        } else if (pwdSenha.getText().equalsIgnoreCase("")) {
            JOptionPane.showMessageDialog(this, "CAMPO EM BRANCO \nINSIRA SUA SENHA NOVAMENTE");
            pwdSenha.requestFocus();
        } else {
            JOptionPane.showMessageDialog(this, " SENHA INVALIDA");
            pwdSenha.setText("");
            pwdSenha.requestFocus();
        }
    }

    public class MaxLengthTextDocument extends PlainDocument {
        //Armazena o número máximo de caracteres para o texto.

        int valor = 10;

        @Override
        public void insertString(int offs, String str, AttributeSet a)
                throws BadLocationException {
            if (str != null && (getLength() + str.length() < valor)) {
                super.insertString(offs, str, a);
            }
        }
    }

    class MyThread extends Thread {

        ImageIcon i = new ImageIcon("C:\\cfgReligadora\\icons\\loading.gif");
        ImageIcon nulo = new ImageIcon("");

        @Override
        public void run() {

            try {

                jLabel9.setIcon(i);
                pwdSenha.setEnabled(false);
                Thread.sleep(500);

                validaUsuario();

            } catch (InterruptedException exc) {
                System.out.println(exc);
            }

            jLabel9.setIcon(nulo);
            pwdSenha.setEnabled(true);
        }
    }

    public void downGradient() {

        UIManager manager = new UIManager();

        LinkedList<Object> a = new LinkedList<>();

        a.add(0.3);

        a.add(0.3);


        a.add(new ColorUIResource(13, 122, 121));

        a.add(new ColorUIResource(17, 111, 122));

        a.add(new ColorUIResource(13, 122, 121));

        UIManager.put("Button.gradient", a);

    }

    public void upGradient() {

        UIManager manager = new UIManager();

        LinkedList<Object> a = new LinkedList<>();

        a.add(0.3);

        a.add(0.3);

        a.add(new ColorUIResource(16, 90, 90));

        a.add(new ColorUIResource(17, 111, 122));

        a.add(new ColorUIResource(16, 90, 90));

        UIManager.put("Button.gradient", a);

    }
}
